package com.cn.sys.frame.util.query;

import java.util.ArrayList;
import java.util.List;
/**
 * 对查询条件的处理
 * @author liuwenjie
 *
 */
public class QueryCondition {

	private List<QueryBean> queryBeanList; //查询条件集合
	private Object[] queryValues;//条件值集合
	private int[] argTypes;//值类型集合，只有当QueryBean中填写了相应的值类型才会有该值
	private String orderSql;
	
	public List<QueryBean> getQueryBeanList() {
        return queryBeanList;
    }

    public void setQueryBeanList(List<QueryBean> queryBeanList) {
        this.queryBeanList = queryBeanList;
    }
    
    public void setQueryBean(QueryBean queryBean) {
        if(queryBeanList == null){
        	setQueryBeanList(new ArrayList<QueryBean>());
        }
        queryBeanList.add(queryBean);
    }
    
    public Object[] getQueryValues() {
		return queryValues;
	}

	public void setQueryValues(Object[] queryValues) {
		this.queryValues = queryValues;
	}

	public int[] getArgTypes() {
		return argTypes;
	}

	public void setArgTypes(int[] argTypes) {
		this.argTypes = argTypes;
	}

	public String getOrderSql() {
		return orderSql;
	}

	public void setOrderSql(String orderSql) {
		this.orderSql = orderSql;
	}

	public String getQueryString() {
    	if(queryBeanList==null||queryBeanList.isEmpty()){
    		return "";
    	}
    	StringBuffer sqlBuffer = new StringBuffer();
    	Object[] values = new Object[queryBeanList.size()];
    	int [] types = new int[queryBeanList.size()];
    	for (int i=0,len=queryBeanList.size(); i<len; i++) {
    		QueryBean qb = queryBeanList.get(i);
			sqlBuffer.append(" ").append(qb.getQueryOperation());
			values[i] = qb.getQueryValue();
			types[i] = qb.getArgType();
		}
    	
    	this.setArgTypes(types);
    	this.setQueryValues(values);
    	return sqlBuffer.toString();
    }
	/*public static void main(String[] args){
		QueryCondition qc = new QueryCondition();
		List<QueryBean> list = new ArrayList<QueryBean>();
		QueryBean qb = new QueryBean();
		qb.setQueryOperation("and a=?");
		qb.setQueryValue("2");
		qb.setQueryKey("a");
		list.add(qb);
		
		QueryBean qb1 = new QueryBean();
		qb1.setQueryOperation("and b=?");
		qb1.setQueryValue("3");
		qb1.setQueryKey("b");
		list.add(qb1);
		qc.setQueryBeanList(list);
		System.out.println(qc.getQueryString());
	}*/
}
